package com.sname.demoribbon.command;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author 宋贤坤
 * @date 2018/11/18 21:10
 */
@Service
public class HelloServiceObserveCommand extends HystrixObservableCommand<String> {
    private RestTemplate restTemplate;

    public HelloServiceObserveCommand(String commandGroupKey, RestTemplate restTemplate) {
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
    }

    protected HelloServiceObserveCommand(HystrixCommandGroupKey group) {
        super(group);
    }

    /**
     * 与前面不一样的是,HHystrixCommand<String>调用只能返回一组结果(即一次数据);
     * 而此处这个HystrixObservableCommand<String>可以返回多组
     * 而且前面是run方法,此处是construct方法
     *
     * @return
     */
    @Override
    protected Observable<String> construct() {
        //创建观察者,观察一个什么事件
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                //如果订阅了才执行这个逻辑,如果没有订阅就算了
                try {
                    if (!subscriber.isUnsubscribed()) {
                        System.out.println("方法执行!");
                        //多个请求用onNext串起来
                        String result = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
                        //订阅方法,onNext(result);是传递请求,将请求返回回去
                        subscriber.onNext(result);
                        //如果有多个方法需要订阅,
                        String result1 = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
                        subscriber.onNext(result1);
                        //当多个方法执行完了后：
                        subscriber.onCompleted();
                        //onCompleted();表示已经执行完毕,要是后面还有OnNext()的话虽然不会报错但是也不得执行了
                    }
                } catch (RestClientException e) {
                    //当这个什么东西出错来着的话,就将错误传出去打印出来
                    subscriber.onError(e);
                }
            }
        });
    }

    /**
     * 服务降级
     * 因为返回的都是"Observable",所以内部的代码也都差不多
     * 也必须返回的是"Observable"
     * 与前面不同的是,前面是getFallback,此处是resumeWithFallback
     *
     * @return
     */
    @Override
    protected Observable<String> resumeWithFallback() {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext("error");
                        subscriber.onCompleted();
                    }
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        });
    }
}
